package com.yl.ydk.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.utils.LogUtils
import com.google.gson.Gson
import com.tencent.avroom.TXCAVRoomConstants.RENDER_MODE_FULL_FILL_SCREEN
import com.tencent.live2.V2TXLiveDef
import com.tencent.live2.V2TXLivePlayer
import com.tencent.live2.V2TXLivePlayerObserver
import com.tencent.live2.impl.V2TXLivePlayerImpl
import com.tencent.rtmp.TXLiveConstants
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.bean.LiveDetailBean
import com.yl.ydk.presenter.impl.TXCloudVideoActivityImpl
import com.yl.ydk.utils.GlideUtils
import kotlinx.android.synthetic.main.activity_txcloud_video.*


class TXCloudVideoActivity : BaseActivity(), View.OnClickListener {

    private var mLivePlayer: V2TXLivePlayerImpl? = null
    private var data: LiveDetailBean? = null

    private val mPresenter = TXCloudVideoActivityImpl()

    override fun getLayoutId(): Int = R.layout.activity_txcloud_video

    override fun attachView() {

    }

    override fun detachView() {

    }

    @SuppressLint("SetTextI18n")
    override fun initView() {

        rlBack.setOnClickListener(this)
        videoView?.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN)
        videoView.setRenderRotation(TXLiveConstants.RENDER_ROTATION_LANDSCAPE)
        mLivePlayer = V2TXLivePlayerImpl(this)
        mLivePlayer?.setRenderView(videoView)
        data = Gson().fromJson<LiveDetailBean>(
            intent.getStringExtra("data"),
            LiveDetailBean::class.java
        )
        mLivePlayer?.startPlay(intent.getStringExtra("play_url"))
        mLivePlayer?.setRenderFillMode(V2TXLiveDef.V2TXLiveFillMode.V2TXLiveFillModeFill)
        data?.data?.user?.name?.let { tvName.text = it }
        data?.data?.user?.photo?.let { GlideUtils.loadRound(this@TXCloudVideoActivity, it, ivHead) }
        data?.data?.statistic?.view_total?.let { tvViewTotal.text = "$it 人观看" }

    }

    override fun onDestroy() {
        super.onDestroy()
        mLivePlayer?.stopPlay()
        videoView.onDestroy()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBack -> {
                finish()
            }
        }
    }

}